const { log } = require("debug/src/browser");
var express = require("express");
var router = express.Router();

// 引入封装的 mysql模块
const mysql = require("../util/mysql/mysql");

//项目广场
router.post("/projectCenter", function (req, res, next) {
  var t_id = req.body.t_id;
  const p1 = new Promise((res1, rej2) => {
    mysql.data(
      "SELECT id,p_title,p_synopsis,t_name FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id LIMIT 12",
      [],
      function (err, rows, fields) {
        if (err) return;
        res1(rows);
      }
    );
  });
  const p2 = new Promise((res2, rej2) => {
    mysql.data(
      "SELECT c_project FROM my_collect where c_teacher=?",
      [t_id],
      function (err, rows, fields) {
        if (err) return;
        const newArr = rows.filter((item) => item.c_project !== null);
        res2(newArr);
      }
    );
  });
  const p3 = new Promise((res1, rej2) => {
    mysql.data(
      "SELECT id,p_title,p_synopsis,t_name FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id",
      [],
      function (err, rows, fields) {
        if (err) return;
        res1(rows.length);
      }
    );
  });
  Promise.all([p1, p2, p3]).then((resall) => {
    res.send(
      JSON.stringify({
        code: 1,
        project: resall[0],
        collect: resall[1],
        dataLength: resall[2],
      })
    );
  });
});
//分页获取数据
router.post("/projectCenterYe", function (req, res, next) {
  var [t_id, val, keyWord] = [req.body.t_id, req.body.val, req.body.keyWord];
  const p1 = new Promise((res1, rej2) => {
    mysql.data(
      'SELECT id,p_title,p_synopsis,t_name FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id AND p_title LIKE "%' +
        keyWord +
        '%" LIMIT ?,12',
      [(val - 1) * 12],
      function (err, rows, fields) {
        if (err) return;
        res1(rows);
      }
    );
  });
  const p2 = new Promise((res2, rej2) => {
    mysql.data(
      "SELECT c_project FROM my_collect where c_teacher=?",
      [t_id],
      function (err, rows, fields) {
        if (err) return;
        const newArr = rows.filter((item) => item.c_project !== null);
        res2(newArr);
      }
    );
  });
  Promise.all([p1, p2]).then((resall) => {
    res.send(
      JSON.stringify({ code: 1, project: resall[0], collect: resall[1] })
    );
  });
});
//关键字查找
router.post("/projectCenterSecKeyword", function (req, res, next) {
  var [t_id, keyWord] = [req.body.t_id, req.body.keyWord];
  const p1 = new Promise((res1, rej2) => {
    mysql.data(
      'SELECT id,p_title,p_synopsis,t_name FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id AND p_title LIKE "%' +
        keyWord +
        '%" LIMIT 12',
      [],
      function (err, rows, fields) {
        if (err) return;
        res1(rows);
      }
    );
  });
  const p2 = new Promise((res2, rej2) => {
    mysql.data(
      "SELECT c_project FROM my_collect where c_student=?",
      [t_id],
      function (err, rows, fields) {
        if (err) return;
        const newArr = rows.filter((item) => item.c_project !== null);
        res2(newArr);
      }
    );
  });
  const p3 = new Promise((res1, rej2) => {
    mysql.data(
      'SELECT id,p_title,p_synopsis,t_name FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id AND p_title LIKE "%' +
        keyWord +
        '%"',
      [],
      function (err, rows, fields) {
        if (err) return;
        res1(rows.length);
      }
    );
  });
  Promise.all([p1, p2, p3]).then((resall) => {
    res.send(
      JSON.stringify({
        code: 1,
        project: resall[0],
        collect: resall[1],
        dataLength: resall[2],
      })
    );
  });
});
//收藏项目
router.post("/collectProject", function (req, res, next) {
  let [t_id, c_project, collect] = [
    req.body.t_id,
    req.body.c_project,
    req.body.collect,
  ];
  if (collect == 0) {
    mysql.data(
      "DELETE FROM my_collect WHERE c_project=? AND c_teacher=?",
      [c_project, t_id],
      function (err, rows, fields) {
        if (err) return;
        res.send(JSON.stringify({ code: 1 }));
      }
    );
  } else {
    mysql.data(
      "INSERT INTO my_collect (c_project,c_teacher) VALUES(?,?)",
      [c_project, t_id],
      function (err, rows, fields) {
        if (err) return;
        res.send(JSON.stringify({ code: 1 }));
      }
    );
  }
});
//项目详情信息
router.post("/projectData", function (req, res, next) {
  let id = req.body.p_id;
  const p1 = new Promise((res1, rej2) => {
    mysql.data(
      "SELECT id,p_title,t_name,p_dif,p_peopleNum,p_time,p_synopsis,p_require FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id WHERE ?",
      [id],
      function (err, rows, fields) {
        if (err) return;
        res1(rows[0]);
      }
    );
  });
  const p2 = new Promise((res2, rej2) => {
    mysql.data(
      "SELECT * FROM leave_words WHERE w_type=1 AND w_to=?",
      [id],
      function (err, rows, fields) {
        if (err) return;
        res2(rows.reverse());
      }
    );
  });
  Promise.all([p1, p2]).then((resall) => {
    res.send(
      JSON.stringify({
        code: 1,
        projectData: resall[0],
        projectLeaveWord: resall[1],
      })
    );
  });
});
//写留言
router.post("/writeLeaveWord", function (req, res, next) {
    let intoData = [
      req.body.u_id,
      req.body.w_main,
      req.body.w_time,
      req.body.w_type,
      req.body.w_to,
    req.body.to_type
    ];
    mysql.data(
      "SELECT t_name FROM teacher_mes where t_id=?",
      [req.body.u_id],
      function (err, rows1, fields) {
        if (err) return;
        intoData.push(rows1[0].t_name);
        mysql.data(
          "INSERT INTO leave_words (u_id,w_main,w_time,w_type,w_to,to_type,w_name,u_type) VALUES (?,?,?,?,?,?,?,2)",
          intoData,
          function (err, rows2, fields) {
            if (err) return;
            res.send(JSON.stringify({ code: 1 }));
          }
        );
      }
    );
    
  });
//我的收藏
router.post("/myCollect", function (req, res, next) {
  var t_id = req.body.t_id;
  mysql.data(
    "SELECT c_project FROM my_collect where c_teacher=?",
    [t_id],
    function (err, rows, fields) {
      if (err) return;
      const newArr = rows.filter((item) => item.c_project !== null);
      let collectStr = "";
      newArr.forEach((e) => {
        collectStr = collectStr + "," + e.c_project;
      });
      const collectArr = collectStr.substring(1);
      mysql.data(
        "SELECT id,p_title,p_synopsis,t_name FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id WHERE id IN (" +
          collectArr +
          ")",
        [],
        function (err, rows, fields) {
          if (err) return;
          rows.forEach((e) => {
            e.collect = 1;
          });
          res.send(
            JSON.stringify({
              code: 1,
              project: rows,
            })
          );
        }
      );
    }
  );
});
//我的学生
router.post("/studentList", function (req, res, next) {
    const t_id = req.body.t_id
    mysql.data(
      "SELECT * FROM student_mes where myTeacher_id=?",
      [t_id],
      function (err, rows, fields) {
        if (err) return;
        res.send(
          JSON.stringify({
            code: 1,
            studentList: rows,
          })
        );
      }
    );
  });
//队伍详情
router.post("/getTemeParticulars", function (req, res, next) {
  const [id, team_id] = [req.body.p_id, req.body.team_id];
  const p1 = new Promise((res1, rej2) => {
    mysql.data(
      "SELECT id,p_title,t_name,p_dif,p_peopleNum,p_time,p_synopsis,p_require FROM project a INNER JOIN project_mainmas b INNER JOIN teacher_mes c ON b.p_id=a.id AND a.p_teacher_id=c.t_id WHERE id=?",
      [id],
      function (err, rows, fields) {
        if (err) return;
        res1(rows[0]);
      }
    );
  });
  const p2 = new Promise((res2, rej2) => {
    const team = {};
    mysql.data(
      "SELECT s_id,s_name,cap FROM team_member a INNER JOIN student_mes b ON a.student_id = b.s_id WHERE team_Id=?",
      [team_id],
      function (err, rows, fields) {
        if (err) return;
        rows.forEach((e, v) => {
          if (e.cap == 1) {
            team.cap = rows[v].s_name;
            team.cap_id=rows[v].s_id
          }
        });
        const newRows = rows.filter((item) => item.cap !== 1);
        team.t_member = newRows;
        mysql.data(
          "SELECT notice FROM team WHERE team_id=?",
          [team_id],
          function (err, rows, fields) {
            if (err) return;
            team.notice = rows[0].notice;
            res2(team);
          }
        );
      }
    );
  });
  Promise.all([p1, p2]).then((resall) => {
    res.send(
      JSON.stringify({
        code: 1,
        project: resall[0],
        team: resall[1],
      })
    );
  });
});
//我的信息
router.post("/MyMes", function (req, res, next) {
    const t_id = req.body.t_id;
    //个人信息
    const p1 = new Promise((res1, rej2) => {
      mysql.data(
        "SELECT * FROM teacher_mes WHERE t_id=?",
        [t_id],
        function (err, rows, fields) {
          if (err) return;
          res1(rows[0]);
        }
      );
    });
    // 获奖情况
    const p2 = new Promise((res2, rej2) => {
      mysql.data(
        "SELECT * FROM awards WHERE a_type=2 AND a_user_id=?",
        [t_id],
        function (err, rows, fields) {
          if (err) return;
          res2(rows);
        }
      );
    });
    // 我的项目
    const p3 = new Promise((res3, rej3) => {
      mysql.data(
          "SELECT id,p_title,p_synopsis,p_type FROM project a INNER JOIN project_mainmas b ON a.id=b.p_id WHERE p_teacher_id=?",
          [t_id],
          function (err, rows, fields) {
              if (err) return; 
            res3(rows);
          }
        );
    });
    Promise.all([p1, p2, p3]).then((resall) => {
      res.send(
        JSON.stringify({
          code: 1,
          teacherMes: {
            mes: resall[0],
            awards: resall[1],
            project: resall[2],
          },
        })
      );
    });
  });
//项目筛选
router.post("/projectTypeList", function (req, res, next) {
  const [t_id,p_type] = [req.body.t_id,req.body.p_type];
  // 我的项目
  if(p_type==0){
    mysql.data(
      "SELECT id,p_title,p_synopsis,p_type FROM project a INNER JOIN project_mainmas b ON a.id=b.p_id WHERE p_teacher_id=?",
      [t_id],
      function (err, rows, fields) {
          if (err) return; 
          res.send(
            JSON.stringify({
              code: 1,
              projectList: rows,
            })
          );
      }
    );
  }else{

    mysql.data(
      "SELECT id,p_title,p_synopsis,p_type FROM project a INNER JOIN project_mainmas b ON a.id=b.p_id WHERE p_teacher_id=? AND p_type=?",
      [t_id,p_type],
      function (err, rows, fields) {
          if (err) return; 
          res.send(
            JSON.stringify({
              code: 1,
              projectList: rows,
            })
          );
      }
    );
  }
    
});
//修改信息
router.post("/changeInf", function (req, res, next) {
  const [t_id,t_phone, t_eMail] = [
    req.body.t_id,
    req.body.t_phone,
    req.body.t_eMail,
  ];
  mysql.data(
    "UPDATE teacher_mes SET t_phone=?,t_eMail=? WHERE t_id=?",
    [t_phone, t_eMail, t_id],
    function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//添加新项目
router.post("/addProject", function (req, res, next) {
  const t_id = req.body.t_id
  const projectMes = req.body.projectMes
  mysql.data(
    "INSERT INTO project (p_title,p_type,p_teacher_id,p_expert_id,p_schedule) VALUES (?,0,?,0,0)",
    [projectMes.p_title,t_id],
    function (err, rows, fields) {
      if (err){
        console.log(err);
        return
      };
      mysql.data(
        "INSERT INTO project_mainmas (p_synopsis,p_require,p_time,p_dif,p_peopleNum) VALUES (?,?,?,?,?)",
        [projectMes.p_synopsis,projectMes.p_require,projectMes.p_time,projectMes.p_dif,projectMes.p_peopleNum],
        function (err, rows, fields) {
          if (err) {
            console.log(err);
            return
          };
          res.send(JSON.stringify({ code: 1 }));
        }
      );
    }
  );
});
module.exports = router;
